Vue.component('time-choose', {
  template: "<div class='timeChoose' style='z-index: 99999'>" +
  "<div class='timeBox' id='timeTypeBox'>" +
  "<span class='timeType' index='0'  :class='{default:datetype*1===0}'>按日</span>" +
  "<span class='timeType' index='1' :class='{default:datetype*1===1}'>按周</span>" +
  "<span class='timeType' index='2'  :class='{default:datetype*1===2}'>按月</span>" +
  "</div>" +
  "<p class='next'  @click='reduceTime()'>" +
  "<span>上一{{ datetype*1===0 ? '日' : datetype*1===1 ? '周' :'月' }}</span><img src='../images/left.png' alt=''></p>" +
  "<p class='last' @click='increaseTime()'>" +
  "<img src='../images/right.png' alt=''><span>下一{{ datetype*1===0 ? '日' : datetype*1===1 ? '周' :'月' }}</span>" +
  "</p>" +
  "</div>",
  data: function() {
    return {
      dateBase: 1,
      date: '',
      startTime: '',
      endTime: '',
    }
  },
  props:{
    datetype:{
      default:0
    },
    fromdate:{},
    todate:{}
  },
  created: function() {
    // this.initTime()
  },
  mounted:function(){
    var vm = this;
    // console.log(this.fromdate)
    // console.log(this.todate)
    mui('#timeTypeBox').on('tap','.timeType',function(){
      var index = this.getAttribute('index')*1;
      vm.datetype=index;
      // vm.$parent.dateType=index;
      vm.changeDatabase(index);
    })
  },
  methods:{
    // initTime: function() {
    //   this.date = new Date();
    //   this.startTime=new Date(this.date).format('yyyy-MM-dd')
    //   this.endTime=new Date(this.date).format('yyyy-MM-dd')
    //   this.$parent.dateType=0;
    // },
    //改变 日 或者 周  月
    changeDatabase: function(type) {
      var vm = this;
      this.$parent.dateType =  type*1
      switch(type) {
        case 0:
          // var date=(this.todate+'').replace(/-/g,'/')
          // this.date = new Date(date)
          this.date = new Date()
          this.startTime = new Date(this.date).format('yyyy-MM-dd')
          this.endTime = new Date(this.date).format('yyyy-MM-dd ')
          break;
        case 1:
          // var date=(this.todate+'').replace(/-/g,'/')
          // this.date = new Date(date)
          this.date = new Date()
          var weekday = this.date.getDay();//0:周日，1-6：周一到周六
          if(weekday===0){weekday=7};
          var startTime=new Date().getTime() - (24*60*60*1000)*(weekday-1);
          this.startTime=new Date(startTime).format('yyyy-MM-dd')
          this.endTime = new Date(this.date).format('yyyy-MM-dd')
          break;
        case 2:
          // var date=(this.todate+'').replace(/-/g,'/')
          // this.date = new Date(date)
          this.date = new Date()
          this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
          this.endTime = new Date(this.date).format('yyyy-MM-dd')
          break;
      }
      this.$parent.toDate = this.endTime
      this.$parent.fromDate = this.startTime
      // console.log(this.endTime)
      // console.log(this.startTime)
    },
    //减少时间
    reduceTime: function() {
      var date=(this.todate).replace(/-/g,'/')
      this.date = new Date(date).getTime()
      switch(this.datetype) {
        case 0:
          this.date = this.date - 86400000
          this.startTime = new Date(this.date).format('yyyy-MM-dd')
          this.endTime = new Date(this.date).format('yyyy-MM-dd')
          break;
        case 1:
          var weekday = new Date(this.date).getDay();//0:周日，1-6：周一到周六
          if(weekday === 0) weekday = 7;
          this.date = this.date - weekday * 86400000
          this.startTime = new Date(this.date - 6 * 86400000).format('yyyy-MM-dd')
          this.endTime = new Date(this.date).format('yyyy-MM-dd')
          break;
        case 2:
          this.date = this.date - 86400000 * new Date(this.date).format('dd')
          this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
          this.endTime = new Date(this.date).format('yyyy-MM-dd')
          break;
      }
      this.$parent.toDate = this.endTime
      this.$parent.fromDate  = this.startTime

    },
    //增加时间
    increaseTime: function() {
      var date=(this.todate).replace(/-/g,'/')
      this.date = new Date(date).getTime();
      console.log(this.datetype);
      switch(this.datetype) {
        case 0:
          if(new Date(this.date + 86400000).format('yyyyMMdd')*1 === new Date().format('yyyyMMdd')*1) {
            this.date = new Date()
            this.startTime = new Date(this.date).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd ')
          } else if(new Date(this.date + 86400000).format('yyyyMMdd')*1 < new Date().format('yyyyMMdd')*1) {
            this.date = this.date + 86400000
            this.startTime = new Date(this.date).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
          }
          break;
        case 1:
          var weekday = new Date().getDay();//0:周日，1-6：周一到周六
          if(weekday === 0) weekday = 7;
          if(new Date(this.date+weekday*86400000).format('yyyyMMdd')*1  === new Date().format('yyyyMMdd')*1 ) {
            this.date = new Date();
            var startTime=new Date().getTime() - (86400000)*(weekday-1);
            this.startTime=new Date(startTime).format('yyyy-MM-dd')
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
          } else if(new Date(this.date+86400000).format('yyyyMMdd')*1  < new Date().format('yyyyMMdd')*1 ) {
            this.startTime = new Date(this.date+86400000).format('yyyy-MM-dd')
            this.endTime = new Date(this.date+ 7 * 86400000).format('yyyy-MM-dd')
            this.date = this.date+ 7 * 86400000;
          }
          break;
        case 2:
          console.log(new Date(this.date).format('yyyy-MM'))
          console.log(new Date().format('yyyy-MM'))
          if(new Date(this.date).format('yyyy-MM') === new Date().format('yyyy-MM')) {
            console.log(4586486)
            this.date = new Date()
            this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
            this.endTime = new Date().format('yyyy-MM-dd')
          }
          if(new Date(this.date).format('yyyy') < new Date().format('yyyy') || [new Date(this.date).format('yyyy') === new Date().format('yyyy') && (new Date(this.date).format('MM') * 1 + 1) < new Date().format('MM') * 1]) {
            var array = new Date(this.date).format('yyyy-MM').split('-')
            var nextMonthday = 32 - new Date(array[0] * 1, array[1] * 1, 32).getDate()
            this.date = this.date + 86400000 * nextMonthday
            this.startTime = new Date(this.date).format('yyyy-MM-') + '01'
            this.endTime = new Date(this.date).format('yyyy-MM-dd')
          }

          break;
      }
      this.$parent.toDate = this.endTime
      this.$parent.fromDate  = this.startTime
    }
  }


})